package cn.wizzer.app.web.modules.controllers.platform.cms;

import cn.wizzer.app.cms.modules.models.CmsActivity;
import cn.wizzer.app.cms.modules.services.CmsActivityService;
import cn.wizzer.framework.base.Result;
import com.alibaba.dubbo.config.annotation.Reference;
import org.apache.shiro.authz.annotation.RequiresPermissions;
import org.nutz.dao.Chain;
import org.nutz.dao.Cnd;
import org.nutz.ioc.loader.annotation.Inject;
import org.nutz.ioc.loader.annotation.IocBean;
import org.nutz.lang.Strings;
import org.nutz.lang.Times;
import org.nutz.mvc.annotation.At;
import org.nutz.mvc.annotation.Ok;
import org.nutz.mvc.annotation.Param;

/**
 * @author zl
 * @date 2020/7/1 11:00
 */
@IocBean
@At("/platform/cms/activity")
public class CmsActivityController {

    @Inject
    @Reference
    private CmsActivityService cmsActivityService;

    @At("/")
    @Ok("beetl:/platform/cms/activity/index.html")
    @RequiresPermissions("cms.activity.list")
    public void index() {

    }

    /**
     * 活动列表
     *
     * @param searchKeyword
     * @param searchDate
     * @param isLongActivity
     * @param pageNumber
     * @param pageSize
     * @return
     */
    @At
    @Ok("json:full")
    @RequiresPermissions("cms.activity.list")
    public Result data(@Param("searchKeyword") String searchKeyword,
                       @Param("searchDate") String[] searchDate,
                       @Param("isLongActivity") Integer isLongActivity,
                       @Param("pageNumber") int pageNumber,
                       @Param("pageSize") int pageSize) {
        Cnd cnd = Cnd.where("delFlag", "=", 0);
        if (Strings.isNotBlank(searchKeyword)) {
            cnd.and("activityName", "like", "%" + searchKeyword + "%");
        }
        if (searchDate != null && searchDate.length > 0) {
            if (isLongActivity != null && isLongActivity == 1) {
                cnd.and("startDate", ">=", Times.format("yyyy-MM-dd", Times.D(searchDate[0])))
                        .and("endDate", "<=", Times.format("yyyy-MM-dd", Times.D(searchDate[1])));
            } else {
                cnd.and("ctAt", ">=", Times.d2TS(Times.D(searchDate[0])))
                        .and("ctAt", "<=", Times.d2TS(Times.D(searchDate[1])));
            }
        }
        if (isLongActivity != null) {
            cnd.and("isLongActivity", "=", isLongActivity);
        }
        cnd.orderBy("ctAt", "desc");
        return Result.success().addData(cmsActivityService.listPage(pageNumber, pageSize, cnd));
    }

    /**
     * 添加活动
     *
     * @param cmsActivity
     * @return
     */
    @At
    @Ok("json:full")
    @RequiresPermissions("cms.activity.add")
    public Result add(@Param("..") CmsActivity cmsActivity) {
        if (!cmsActivity.getIsLongActivity() && (cmsActivity.getStartDate() == null || cmsActivity.getEndDate() == null)) {
            Result.error("非长期活动请选择时间");
        }
        cmsActivityService.insert(cmsActivity);
        return Result.success();
    }

    /**
     * 修改活动
     *
     * @param cmsActivity
     * @return
     */
    @At
    @Ok("json:full")
    @RequiresPermissions("cms.activity.edit")
    public Result edit(@Param("..") CmsActivity cmsActivity) {
        if (!cmsActivity.getIsLongActivity() && (cmsActivity.getStartDate() == null || cmsActivity.getEndDate() == null)) {
            Result.error("非长期活动请选择时间");
        }
        cmsActivityService.update(cmsActivity);
        return Result.success();
    }

    /**
     * 启、禁用
     *
     * @param id
     * @param status
     * @return
     */
    @At
    @Ok("json:full")
    @RequiresPermissions("cms.activity.edit")
    public Result disabled(@Param("id") String id,
                           @Param("status") Integer status) {
        cmsActivityService.update(Chain.make("disabled", status), Cnd.where("id", "=", id));
        return Result.success();
    }

    /**
     * 删除活动
     *
     * @param id
     * @return
     */
    @At
    @Ok("json:full")
    @RequiresPermissions("cms.activity.del")
    public Result del(@Param("id") String id) {
        cmsActivityService.update(Chain.make("delFlag", 1), Cnd.where("id", "=", id));
        return Result.success();
    }

}
